package game.Actors;
import game.Config.Storage;
import game.Config.TowerConfig;
import game.Config.TowerInfo;
import java.util.ArrayList;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;
import Weapons.Weapon;
/**
 * This class represents a basic Tower with a
 * fire rate, sight radius and a weapon.
 * The tower also contains information about the next tower,
 *  that is the upgrade of this tower. 
 * @author Sebastian Isheden and Victor Beer
 */
public class Tower extends Actor{
	private static ArrayList<Tower> towers = new ArrayList<Tower>();
	private float sightRadius;
	private Weapon weapon;
	private Actor target;
	private TowerInfo nextTower;
	/**
	 * Creates a tower with given parameters.
	 * @param image The image of the tower.
	 * @param position The position of the tower.
	 * @param weapon The weapon this tower will fire with.
	 * @param sightRadius The radius within which this tower can detect enemies.
	 * @param nextTower Information about the upgrade of this tower.
	 */
	public Tower(Image image, Vector2f position,Weapon weapon,float sightRadius,TowerInfo nextTower) {
		super(image, position);
		this.weapon = weapon;
		this.sightRadius = sightRadius;
		this.nextTower = nextTower;
		towers.add(this);
	}


	
	@Override
	protected void update(int delta){
		if(target == null){
			findTarget();
		}else {
			Vector2f shootDirection = target.position.copy().add(new Vector2f(-this.position.x,-this.position.y));
			weapon.fire(this.position.copy(), shootDirection.normalise());
			if(target.position.copy().add(new Vector2f(-this.position.x,-this.position.y)).length() > this.sightRadius){
				this.target = null;

			}else if(target.isDead)
				this.target = null;
		}
		this.weapon.update(delta);
	}
	private void findTarget() {
		for(Enemy enemy : Enemy.getEnemyList()){
			if(enemy.position.copy().add(new Vector2f(-this.position.x,-this.position.y)).length() <= this.sightRadius){
				this.target = enemy;
				break;
			}
		}
	}
	/**
	 * @return A list of all the Towers in the game.
	 */
	public static ArrayList<Tower> getTowerList(){
		return towers;
	}
	/**
	 * Removes the tower.
	 */
	@Override
	public void remove(){
		super.remove();
		towers.remove(this);
	}
	/**
	 * Creates a copy of this tower at the given position. 
	 * @param position	The position of the new tower.
	 */
	public void placeTower(Vector2f position){
		new Tower(this.image.copy(),position.copy(),this.weapon.copy(),this.sightRadius,this.nextTower);
	}
	/**
	 * @return The info of the upgrade of this tower.
	 */
	public TowerInfo getUpgradeInfo(){
		return this.nextTower;
	}
	/**
	 * Removes this tower and places the upgrade of this tower in it's place.
	 * @param money The amount of money paid.
	 * @return True - If the cost of the next tower is lower than the parameter money. False otherwise.
	 */
	public boolean upgrade(int money) {
		if(this.nextTower.getCost() > money){
			return false;
		}
		TowerConfig.towerInfoMap.get(this.nextTower).placeTower(this.position.copy());
		this.remove();
		Storage.getPlayer().receiveMoney(-nextTower.getCost());
		return true;
	}
}
